[% setvar title crypt() default salt %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>crypt() default salt</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Mark Dominus &lt;<a href='mailto:mjd@plover.com'>mjd@plover.com</a>&gt;
  Date: 11 Sep 2000
  Last Modified: 23 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 208
  Version: 4
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>A frequently-asked question is how to generate an appropaite random
salt for password encryption.  I propose that Perl generate the salt
automatically if the salt argument is omitted in the call to crypt().</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>At present, crypt() requires two arguments:</p>
<pre>         crypt PLAINTEXT,SALT</pre>
<p>It then passes these arguments directly to the C library crypt()
function.</p>
<p>When encrypting a new password, the programmer is required to generate
a salt at random:</p>
<pre>        @letters = ('A' .. 'Z', 'a' .. 'z', '0' .. '9', '/', '.');
        $salt = $letters[<a href='mailto:rand@letters'>rand@letters</a>] . $letters[<a href='mailto:rand@letters'>rand@letters</a>];
        $passwd = crypt($passwd, $salt);</pre>
<p>This is inconvenient and nonportable.  It's also nonobvious: people
frequently ask in the newsgroups how to do it.  I propose that if the
SALT argument is omitted, Perl should generate an appropriate salt
internally and use that.</p>
<pre>        $passwd = crypt($passwd);    # Same as above</pre>
<p>On systems where the password format is different, Perl can do the
appropriate thing.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>For the standard DES-based crypt, the implementation is
straightforward trivial. Perl already has many functions that take an
optional argument, and the C internals of the random-salt generator
are well-known.</p>
<p>Details will vary for systems using alternative password hashing
schemes.  On some systems, no salt need be generated.  These can be
taken care of with a suitably ifdef'ed section of code if necessary.</p>
<p>If the random number generator has not yet been seeded, Perl should
seed it.</p>
<p>Michael Schwern has developed a partial demonstration implementation
in pure Perl.  It is available from</p>
<pre>         <a href='http://www.pobox.com/~schwern/src/RFC-Prototype-0.01.tar.gz' target='_blank'>www.pobox.com</a></pre>
<p>It has been suggested that <code>crypt()</code> should have a private random
number generator, to avoid interfering with the sequence of numbers
produced by rand().  This would significantly complicate the
implementation, and I believe it is probably unnecessary.  See the
REFERENCES for details.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p><code>crypt()</code> with only one argument is presently a compile-time error,
so there are probably few translation issues.  The meaning of this
program will change:</p>
<pre>        $&quot; = ', ';
        $code = &quot;crypt(@ARGV)&quot;;
        eval $code;
        die $@ if $@;</pre>
<p>But I don't think this is anything to worry about---it should fall
into the &quot;other 5%&quot; category.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>perlfunc manpage for discussion of crypt()</p>
<p>crypt(3)</p>
<p><a href='http://dev.perl.org/archive?35:mss:4500:200009:lmemkmdbnocclmnnijmc' target='_blank'>dev.perl.org</a></p>
</div>
